//
//  PointClassViewController.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/21.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "PointClassViewController.h"
#import "GridView.h"
#import "EBDate.h"
#import "PointClassListViewController.h"

@interface PointClassViewController ()<TeachBottomViewDelegate>

@property(nonatomic,weak)GridView *gridView;

@property(nonatomic,strong)NSMutableArray *items;

@property(nonatomic,strong)NSMutableDictionary *dataDict;


@end

@implementation PointClassViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.view.backgroundColor = BackGroundColor;
    
    self.title = @"约课";
    
    [self createSubView];
    
    [self loadData];
    
    [NotificationCenter addObserver:self selector:@selector(teachClassViewClick:) name:@"HaveClassClick" object:nil];
    
    [NotificationCenter addObserver:self selector:@selector(loadData) name:@"ReloadWeekDay" object:nil];
    
    [NotificationCenter addObserver:self selector:@selector(loadData) name:@"GridClassTypeChange" object:nil];
}


-(void)createSubView{
    
    GridView *gridView = [[GridView alloc] init];
    
    gridView.gridType = @"Point";
    
    gridView.teachBottomView.teachBottomViewDelegate = self;
    
    [gridView.teachBottomView.functionBtn addTarget:self action:@selector(functionBtnClick:)];
    
    [self.view addSubview:gridView];
    
    self.gridView = gridView;
    
    [gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
}



-(void)loadData{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    if ([self.gridView.classType isEqualToString:@"团课"]){
        
        [params setObject:@"0" forKey:@"Type"];
    }
    else{
        
        [params setObject:@"1" forKey:@"Type"];
    }
    
    NSArray *yearArray = [EBDate getFirstAndLastDayOfThisYear];
    
    [params setObject:[yearArray firstObject] forKey:@"BeginDate"];
    
    [params setObject:[yearArray lastObject] forKey:@"EndDate"];
    
    [params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",100] forKey:@"rows"];
    
    [HTTPTool GET:GET_CoursePlanListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  self.items = [[NSMutableArray alloc] init];
                  
                  for (NSDictionary *dict in resultArray) {
                      
                      NSMutableArray *item = [NSMutableArray arrayWithArray:[dict objectForKey:@"Items"]];
                      
                      [self.items addObjectsFromArray:item];
                      
                  }
                  
                  [self handleData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}



/**
 处理数据
 */
-(void)handleData{
    
    [self.dataDict removeAllObjects];
    
    for (NSDictionary *dict in self.items) {
        
        NSString *BeginDateTime = [dict objectForKey:@"BeginDateTime"];
        
        //根据开始时间计算
        NSRange range = NSMakeRange(0, 10);
        
        NSString *key = [BeginDateTime substringWithRange:range];
        
        NSMutableArray *items = [self.dataDict objectForKey:key];
        
        if (!items) {
            
            items = [[NSMutableArray alloc] init];
        }
        
        [items addObject:dict];
        
        [self.dataDict setObject:items forKey:key];
    }
    
    
    self.gridView.weekDayCollectionView.hasTeachDict = self.dataDict;
    
    NSLog(@"dataDict = %@",self.dataDict);
}


/**
 功能按钮点击
 */
-(void)functionBtnClick:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    //显示按钮
    if (btn.selected) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            if ([self.gridView.classType isEqualToString:@"团课"]) {
                
                self.gridView.teachBottomView.titlesArray = nil;
            }
            else{
                
                self.gridView.teachBottomView.titlesArray = [NSMutableArray arrayWithObjects:@"设置休息", nil];
            }
            
        }];
        
    }
    else{
        
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.gridView.teachBottomView clearViews];
        }];
    }
}


/**
 功能按钮点击
 @param title title
 */
-(void)btnClickWithTitle:(NSString *)title{
        
    if ([title isEqualToString:@"设置休息"]){
        
        
    }
}


-(void)teachClassViewClick:(NSNotification *)sender{
    
    NSDictionary *userInfo = sender.userInfo;
    
    NSString *dayTime = [userInfo objectForKey:@"dayTime"];
    
    NSString *rowStr = [userInfo objectForKey:@"rowStr"];
    
    PointClassListViewController *teachClassListVC = [[PointClassListViewController alloc] init];
    
    teachClassListVC.date = dayTime;
    
    teachClassListVC.rowStr = rowStr;
    
    teachClassListVC.classType = self.gridView.classType;
    
    [self.navigationController pushViewController:teachClassListVC animated:YES];
    
}

#pragma mark DataDict
-(NSMutableDictionary *)dataDict{
    
    if (!_dataDict) {
        
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}

-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}

@end
